<?php

/**
 * exportToLuna actions.
 *
 * @package    LIAnnot
 * @subpackage exportToLuna
 * @author     Christian DRAMSY
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class exportToLunaActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
	$this->project_list = Doctrine::getTable('Annoset')
		->createQuery('a')
		->where("a.user_name = ?",$this->getUser()->getUserName())
		->execute();   	
  }
  
  /**
   * Executes exportProject action
   * 
   * @param sfWebRequest $request
   */
  public function executeExportProject(sfWebRequest $request) {
  	$filename = $request->getParameter("filename");
  	$id = $request->getParameter("id");
  	if(!empty($filename) && !empty($id) ) {
  		$annoset = Doctrine::getTable('Annoset')->findOneById($id);
  		if(!empty($annoset ) && $annoset->getUserName() == $this->getUser()->getUserName()) {
		  	
  			$exportedFile = sfConfig::get('sf_upload_dir')."/exported_file/";
  			UseFulTools::cleanDirectory($exportedFile);
  			
  			//	Export data from database to Paula format in the project folder
		  	$projectName = Paula::databaseToPaula($request->getParameter("id"));
		  	// 	Export data from PAULA format to the MEDIA/LUNA format file.
		  	Paula::paulaToMediaLuna($projectName,$filename);
		  	$this->link = "/uploads/exported_file/$filename.xml";
		  	$this->projectName = $projectName;
		  	$this->filename = $filename;
		  	$this->fileFolder = "/var/www/LIAnnot/web/".$this->link;
		  	$this->success = true;
		  	
  		} else {
  			$this->getUser()->setFlash("error","You can't export this project");
  			$this->redirect("/frontend_dev.php/exportToLuna");
  		}
  	} else {
  		$this->success = false;
  	}
  }
  
  public function executeGetFirstMediaFile(sfWebRequest $request) {
  	$filename = $request->getParameter("filename");
  	$id_annoset = $request->getParameter("id");
  	if(!empty($filename) && !empty($id_annoset) ) {
		$annoset = Doctrine::getTable('Annoset')->findOneById($id_annoset);
		if(!empty($annoset)) {
			$this->link = sfConfig::get('sf_upload_dir')."/initMediaFiles/".$annoset->getName().".xml";
			
		  	$this->filename = $filename;
		  	$this->fileFolder = $this->link;
		} else {
			die("error");
		}
  	} else {
  		echo "request error";
  	}
  }
  
  public function executeSaveToPaula(sfWebRequest $request) {
  	
  	$id = $request->getParameter("id");
  	if(!empty($id)) {
  		$projectName = Paula::databaseToPaula($id);
  	}
  	return sfView::NONE;
  }
}
